<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Repositories\AdminUsersRepository;
use App\Util\Result;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    private $adminEntity;
    public function __construct(Request $req,AdminUsersRepository $admin){
        $this->req = $req;
        $this->adminEntity = $admin;
    }

    public function login(){
        return view('admin.login.login');
    }
    public function logout(){
        $this->req->session()->flush();
        return redirect('admin/login');
    }

    public function postLogin(){
        $data = $this->req->all();
        if(!$data['name'] || !$data['password']){
            return Result::failure('参数错误！')->getResult();
        }
        $info = $this->adminEntity->findBy('account',$data['name']);

        if(empty($info)){
            return Result::failure('您输入的用户名密码错误！')->getResult();
        }

        if(!password_verify($data['password'],$info->password) ){
            return Result::failure('您输入的用户名密码错误！')->getResult();
        }
        $this->req->session()->flush();
        session(['admin_info'=>$info]);
        return Result::success('登录成功')->getResult();
    }
}
